variable "tc_secret_id" {
  type    = string
  default = "${env("TENCENTCLOUD_ACCESS_KEY")}"
}

variable "tc_secret_key" {
  type    = string
  default = "${env("TENCENTCLOUD_SECRET_KEY")}"
}

variable "ansible_vault_pass" {
  type    = string
  default = "${env("ANSIBLE_VAULT_PASS")}"
}

variable "ansible_playbook_file" {
  type    = string
  default = "${env("ANSIBLE_PLAYBOOK_FILE")}"
}

variable "ansible_playbook_dir" {
  type    = string
  default = "${env("ANSIBLE_PLAYBOOK_DIR")}"
}

variable "ansible_group_vars_dir" {
  type    = string
  default = "${env("ANSIBLE_GROUP_VARS_DIR")}"
}

variable "ansible_extra_arguments" {
  type    = string
  default = "${env("ANSIBLE_EXTRA_ARGUMENTS")}"
}

variable "ansible_extra_vars" {
  type    = string
  default = "${env("ANSIBLE_EXTRA_VARS")}"
}

source "tencentcloud-cvm" "autogenerated_1" {
  associate_public_ip_address = true
  disk_type                   = "CLOUD_PREMIUM"
  image_name                  = "centos7-basic-{{timestamp}}"
  instance_type               = "SA2.MEDIUM4"
  internet_max_bandwidth_out  = 2
  region                      = "ap-beijing"
  secret_id                   = "${var.tc_secret_id}"
  secret_key                  = "${var.tc_secret_key}"
  #security_group_id           = "sg-5mn1s3xh"
  source_image_id             = "img-oikl1tzv"
  ssh_username                = "root"
  #subnet_id                   = "subnet-h5mz612h"
  #vpc_id                      = "vpc-mz4iixkm"
  zone                        = "ap-beijing-6"
}

build {
  sources = ["source.tencentcloud-cvm.autogenerated_1"]
  provisioner "shell" {
    expect_disconnect = false
    pause_before      = "20s"
    inline = [
      "yum install -y python3 python3-pip",
      "pip3 install -U pip",
      "pip3 install netaddr pbr hvac jmespath ruamel.yaml",
      "pip3 install ansible"
    ]
  }
  provisioner "ansible-local" {
    role_paths    = [
      "../roles/init-node-centos7",
      "../roles/install-zabbix"
    ]
    group_vars    = "../group_vars"
    playbook_dir  = "../playbooks"
    playbook_file = "../playbooks/init-node-centos7-tencentcloud.yml"
    #inventory_groups = 
    #command =
    #extra_arguments = [
      #"--vault-password-file=/bin/cat",
      #"user `ansible_extra_arguments`}}",
    #  "--extra-vars deploy_env=groupclass"
    #]
  }
}
